package com.diao.flink.windowAndTimer;

import org.apache.commons.lang.time.FastDateFormat;
import org.apache.flink.api.common.eventtime.*;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.AssignerWithPeriodicWatermarks;
import org.apache.flink.streaming.api.functions.source.SourceFunction;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.SlidingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.assigners.SlidingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

import java.util.concurrent.TimeUnit;

/**
 * @author: chenzhidiao
 * @date: 2021/2/3 17:05
 * @description: 为每条消息添加时间戳信息，模拟延迟消息，也就是第19秒才接收到第13秒发送第消息
 * @version: 1.0
 *
 * 结果：
 * (flink,1)
 * (flink,3)
 * (flink,1)
 *
 * 正确结果
 * (flink,2)
 * (flink,3)
 * (flink,1)
 *
 * 和正确结果不一致
 */
public class WindowWordCountWithEventTimeDelayed {
    public static void main(String[] args) throws Exception {

        StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironmentWithWebUI(new Configuration());
        env.setParallelism(1);

        //模仿延迟数据，在13秒时发送一条，16秒时一条，19秒时一条
        DataStreamSource<String> streamSource = env.addSource(new TestSourceDelayedWithTimestamp());

        streamSource.flatMap(new FlatMapFunction<String, Tuple2<String, Long>>() {

            @Override
            public void flatMap(String s, Collector<Tuple2<String, Long>> collector) throws Exception {
                //将每条数据按逗号分割，第一个字段时单词，第二个字段时时间戳
                String[] fields = s.split(",");
                collector.collect(Tuple2.of(fields[0],Long.valueOf(fields[1])));
            }
        }).assignTimestampsAndWatermarks( WatermarkStrategy //定义水位策略
                .forGenerator((ctx) -> new PeriodicWatermarkGenerator())
                .withTimestampAssigner((ctx) -> new TimeStampExtractor()))
                .keyBy(tuple -> tuple.f0)
                .window(SlidingEventTimeWindows.of(Time.seconds(10), Time.seconds(5)))
                .process(new SumProcessWindowFunction1())
                .print().setParallelism(1);

        env.execute("wordCountWithWindow");


    }

    /**
     * watermark的生成方式
     */
    private static class PeriodicWatermarkGenerator implements WatermarkGenerator<Tuple2<String, Long>> {


        //每来一条事件数据调用一次，可以检查或者记录事件的时间戳，或者也可以基于事件数据本身去生成 watermark
        @Override
        public void onEvent(Tuple2<String, Long> event, long eventTimestamp, WatermarkOutput watermarkOutput) {
            //System.out.println(eventTimestamp);
        }

        //周期性地效用，也许会生成新的 watermark，也许不会
        //调用此方法生成 watermark 的时间间隔由 ExecutionFonfig.getAutoWatermarkInterval()决定
        @Override
        public void onPeriodicEmit(WatermarkOutput watermarkOutput) {
            watermarkOutput.emitWatermark(new Watermark(System.currentTimeMillis()));

        }
    }

    /**
     * 指定EventTime字段
     */
    private static class TimeStampExtractor implements TimestampAssigner<Tuple2<String,Long>>{

        @Override
        public long extractTimestamp(Tuple2<String, Long> element, long recordTimestamp) {
            return element.f1;
        }
    }
}
class SumProcessWindowFunction1 extends ProcessWindowFunction<Tuple2<String,Long>,Tuple2<String,Integer>, String, TimeWindow> {

    FastDateFormat dateformat = FastDateFormat.getInstance("HH:mm:ss");

    @Override
    public void process(String key,  Context context,  Iterable<Tuple2<String, Long>> elements,  Collector<Tuple2<String, Integer>> out) throws Exception {

//        System.out.println("当前系统时间："+dateformat.format(System.currentTimeMillis()));
//        System.out.println("窗口处理时间："+dateformat.format(context.currentProcessingTime()));
//
//        System.out.println("窗口开始时间："+dateformat.format(context.window().getStart()));
//        System.out.println("窗口结束时间："+dateformat.format(context.window().getEnd()));
//
//        System.out.println("=====================================================");

        int count = 0;
        for(Tuple2<String,Long> e :elements){
            //统计单词个数
            count++;
        }
        out.collect(Tuple2.of(key,count));

    }
}

class TestSourceDelayedWithTimestamp implements SourceFunction<String>{
    FastDateFormat dateformat = FastDateFormat.getInstance("HH:mm:ss");

    @Override
    public void run(SourceContext<String> ctx) throws Exception {
        String currentTime = String.valueOf(System.currentTimeMillis());

        //System.out.println(currentTime.substring(currentTime.length()-4));

        while(Integer.valueOf(currentTime.substring(currentTime.length()-4))>100){
            currentTime = String.valueOf(System.currentTimeMillis());
            continue;

        }

        System.out.println("当前时间："+dateformat.format(System.currentTimeMillis()));

        TimeUnit.SECONDS.sleep(13);

        //定义第一条消息
        String event = "flink,"+System.currentTimeMillis();
        //初始化变量event1，值和event一样，包含了当前的时间戳，然后在第19秒发送这条消息，作为延时消息
        String event1 = event;

        ctx.collect(event);

        TimeUnit.SECONDS.sleep(3);

        ctx.collect("flink,"+System.currentTimeMillis());

        TimeUnit.SECONDS.sleep(3);

        ctx.collect(event1);

        TimeUnit.SECONDS.sleep(3000000);
    }

    @Override
    public void cancel() {

    }
}